#include "GUIDataSolidWidget.h"
#include "ui_GUIDataSolidWidget.h"

#include "FITK_Interface/FITKInterfaceCustomData/FITKCDDataSolid.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "OperatorsInterface/TreeEventOperator.h"

using namespace GUI;

GUIDataSolidWidget::GUIDataSolidWidget(Interface::FITKCDDataSolid* obj, EventOper::ParaWidgetInterfaceOperator* oper, QWidget* parent) :
	GUIWidgetBase(parent), _oper(oper), _obj(obj)
{
	_ui = new Ui::GUIDataSolidWidget();
	_ui->setupUi(this);

	init();
}

GUIDataSolidWidget::~GUIDataSolidWidget()
{
	if (_ui) {
		delete _ui;
		_ui = nullptr;
	}
}

void GUIDataSolidWidget::init()
{
	_ui->comboBox_Solid_Type->addItems(QStringList() << "Fixed Wall" << "Movable Rigid Body" << "Solid Constitutive");
	_ui->comboBox_Material->addItems(QStringList() << "copper" << "steel");
	_ui->comboBox_Rigid_Type->addItems(QStringList() << "dynamic" << "stationary");

	setDataToWidget();
}

void GUIDataSolidWidget::setDataToWidget()
{
	_ui->lineEdit_Name->setText(_obj->getDataObjectName());
	_ui->comboBox_Solid_Type->setCurrentIndex(_obj->getSolid_Type());
	_ui->comboBox_Material->setCurrentIndex(_obj->getMaterial());
	_ui->doubleSpinBox_Density->setValue(_obj->getDensity());
	_ui->comboBox_Rigid_Type->setCurrentIndex(_obj->getRigid_Type());
	_ui->doubleSpinBox_Mass->setValue(_obj->getMass());
	_ui->checkBox_Custom_Defined_Inertia_Matrix->setChecked(_obj->getCustom_Defined_Inertia_Matrix());
	_ui->doubleSpinBox_Centroid0->setValue(_obj->getCentroid()[0]);
	_ui->doubleSpinBox_Centroid1->setValue(_obj->getCentroid()[1]);
	_ui->doubleSpinBox_Centroid2->setValue(_obj->getCentroid()[2]);
	_ui->doubleSpinBox_Inertia_Matrix_xx->setValue(_obj->getInertia_Matrix_xx());
	_ui->doubleSpinBox_Inertia_Matrix_yy->setValue(_obj->getInertia_Matrix_yy());
	_ui->doubleSpinBox_Inertia_Matrix_zz->setValue(_obj->getInertia_Matrix_zz());
	_ui->doubleSpinBox_Inertia_Matrix_xy->setValue(_obj->getInertia_Matrix_xy());
	_ui->doubleSpinBox_Inertia_Matrix_xz->setValue(_obj->getInertia_Matrix_xz());
	_ui->doubleSpinBox_Inertia_Matrix_yz->setValue(_obj->getInertia_Matrix_yz());
	_ui->doubleSpinBox_Linear_Velocity0->setValue(_obj->getLinear_Velocity()[0]);
	_ui->doubleSpinBox_Linear_Velocity1->setValue(_obj->getLinear_Velocity()[1]);
	_ui->doubleSpinBox_Linear_Velocity2->setValue(_obj->getLinear_Velocity()[2]);
	_ui->doubleSpinBox_Angular_Velocity0->setValue(_obj->getAngular_Velocity()[0]);
	_ui->doubleSpinBox_Angular_Velocity1->setValue(_obj->getAngular_Velocity()[1]);
	_ui->doubleSpinBox_Angular_Velocity2->setValue(_obj->getAngular_Velocity()[2]);
	_ui->doubleSpinBox_Initial_Euler_Angles0->setValue(_obj->getInitial_Euler_Angles()[0]);
	_ui->doubleSpinBox_Initial_Euler_Angles1->setValue(_obj->getInitial_Euler_Angles()[1]);
	_ui->doubleSpinBox_Initial_Euler_Angles2->setValue(_obj->getInitial_Euler_Angles()[2]);

	_ui->spinBox_Type_of_Element->setValue(_obj->getType_of_Element());
	_ui->spinBox_In_or_Out_Normalvector->setValue(_obj->getIn_or_Out_Normalvector());
	_ui->doubleSpinBox_Specified_Axis0->setValue(_obj->getSpecified_Axis()[0]);
	_ui->doubleSpinBox_Specified_Axis1->setValue(_obj->getSpecified_Axis()[1]);
	_ui->doubleSpinBox_Specified_Axis2->setValue(_obj->getSpecified_Axis()[2]);
	_ui->checkBox_Constrain_DOF->setChecked(_obj->getConstrain_DOF());
	_ui->checkBox_Freeze_Movement_X->setChecked(_obj->getFreeze_Movement_X());
	_ui->checkBox_Freeze_Movement_Y->setChecked(_obj->getFreeze_Movement_Y());
	_ui->checkBox_Freeze_Movement_Z->setChecked(_obj->getFreeze_Movement_Z());
	_ui->checkBox_Freeze_Rotation_X->setChecked(_obj->getFreeze_Rotation_X());
	_ui->checkBox_Freeze_Rotation_X->setChecked(_obj->getFreeze_Rotation_X());
	_ui->checkBox_Freeze_Rotation_Z->setChecked(_obj->getFreeze_Rotation_Z());
}


void GUIDataSolidWidget::on_lineEdit_Name_editingFinished()
{
	_obj->setDataObjectName(_ui->lineEdit_Name->text());

	auto treeOper = Core::FITKOperatorRepo::getInstance()->getOperatorT<EventOper::TreeEventOperator>("ModelTreeEvent");
	if (treeOper != nullptr)
	{
		treeOper->updateTree();
	}
}
void GUIDataSolidWidget::on_comboBox_Solid_Type_activated(int index)
{
	_obj->setSolid_Type(index);
}
void GUIDataSolidWidget::on_comboBox_Material_activated(int index)
{
	_obj->setMaterial(index);
}
void GUIDataSolidWidget::on_doubleSpinBox_Density_valueChanged(double value)
{
	_obj->setDensity(value);
}
void GUIDataSolidWidget::on_comboBox_Rigid_Type_activated(int index)
{
	_obj->setRigid_Type(index);
}
void GUIDataSolidWidget::on_doubleSpinBox_Mass_valueChanged(double value)
{
	_obj->setMass(value);
}
void GUIDataSolidWidget::on_checkBox_Custom_Defined_Inertia_Matrix_stateChanged(int state)
{
	_obj->setCustom_Defined_Inertia_Matrix(state);
}
void GUIDataSolidWidget::on_doubleSpinBox_Centroid0_valueChanged(double value)
{
	auto d = _obj->getCentroid();
	d[0] = value;
	_obj->setCentroid(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Centroid1_valueChanged(double value)
{
	auto d = _obj->getCentroid();
	d[1] = value;
	_obj->setCentroid(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Centroid2_valueChanged(double value)
{
	auto d = _obj->getCentroid();
	d[2] = value;
	_obj->setCentroid(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Inertia_Matrix_xx_valueChanged(double value)
{
	_obj->setInertia_Matrix_xx(value);
}
void GUIDataSolidWidget::on_doubleSpinBox_Inertia_Matrix_yy_valueChanged(double value)
{
	_obj->setInertia_Matrix_yy(value);
}
void GUIDataSolidWidget::on_doubleSpinBox_Inertia_Matrix_zz_valueChanged(double value)
{
	_obj->setInertia_Matrix_zz(value);
}
void GUIDataSolidWidget::on_doubleSpinBox_Inertia_Matrix_xy_valueChanged(double value)
{
	_obj->setInertia_Matrix_xy(value);
}
void GUIDataSolidWidget::on_doubleSpinBox_Inertia_Matrix_xz_valueChanged(double value)
{
	_obj->setInertia_Matrix_xz(value);
}
void GUIDataSolidWidget::on_doubleSpinBox_Inertia_Matrix_yz_valueChanged(double value)
{
	_obj->setInertia_Matrix_yz(value);
}
void GUIDataSolidWidget::on_doubleSpinBox_Linear_Velocity0_valueChanged(double value)
{
	auto d = _obj->getLinear_Velocity();
	d[0] = value;
	_obj->setLinear_Velocity(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Linear_Velocity1_valueChanged(double value)
{
	auto d = _obj->getLinear_Velocity();
	d[1] = value;
	_obj->setLinear_Velocity(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Linear_Velocity2_valueChanged(double value)
{
	auto d = _obj->getLinear_Velocity();
	d[2] = value;
	_obj->setLinear_Velocity(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Angular_Velocity0_valueChanged(double value)
{
	auto d = _obj->getAngular_Velocity();
	d[0] = value;
	_obj->setAngular_Velocity(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Angular_Velocity1_valueChanged(double value)
{
	auto d = _obj->getAngular_Velocity();
	d[1] = value;
	_obj->setAngular_Velocity(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Angular_Velocity2_valueChanged(double value)
{
	auto d = _obj->getAngular_Velocity();
	d[2] = value;
	_obj->setAngular_Velocity(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Initial_Euler_Angles0_valueChanged(double value)
{
	auto d = _obj->getInitial_Euler_Angles();
	d[0] = value;
	_obj->setInitial_Euler_Angles(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Initial_Euler_Angles1_valueChanged(double value)
{
	auto d = _obj->getInitial_Euler_Angles();
	d[1] = value;
	_obj->setInitial_Euler_Angles(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Initial_Euler_Angles2_valueChanged(double value)
{
	auto d = _obj->getInitial_Euler_Angles();
	d[2] = value;
	_obj->setInitial_Euler_Angles(d);
}

void GUIDataSolidWidget::on_spinBox_Type_of_Element_valueChanged(int value)
{
	_obj->setType_of_Element(value);
}
void GUIDataSolidWidget::on_spinBox_In_or_Out_Normalvector_valueChanged(int value)
{
	_obj->setIn_or_Out_Normalvector(value);
}
void GUIDataSolidWidget::on_doubleSpinBox_Specified_Axis0_valueChanged(double value)
{
	auto d = _obj->getSpecified_Axis();
	d[0] = value;
	_obj->setSpecified_Axis(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Specified_Axis1_valueChanged(double value)
{
	auto d = _obj->getSpecified_Axis();
	d[1] = value;
	_obj->setSpecified_Axis(d);
}
void GUIDataSolidWidget::on_doubleSpinBox_Specified_Axis2_valueChanged(double value)
{
	auto d = _obj->getSpecified_Axis();
	d[2] = value;
	_obj->setSpecified_Axis(d);
}
void GUIDataSolidWidget::on_checkBox_Constrain_DOF_stateChanged(int state)
{
	_obj->setConstrain_DOF(state);
}
void GUIDataSolidWidget::on_checkBox_Freeze_Movement_X_stateChanged(int state)
{
	_obj->setFreeze_Movement_X(state);
}
void GUIDataSolidWidget::on_checkBox_Freeze_Movement_Y_stateChanged(int state)
{
	_obj->setFreeze_Movement_Y(state);
}
void GUIDataSolidWidget::on_checkBox_Freeze_Movement_Z_stateChanged(int state)
{
	_obj->setFreeze_Movement_Z(state);
}
void GUIDataSolidWidget::on_checkBox_Freeze_Rotation_X_stateChanged(int state)
{
	_obj->setFreeze_Rotation_X(state);
}
void GUIDataSolidWidget::on_checkBox_Freeze_Rotation_Y_stateChanged(int state)
{
	_obj->setFreeze_Rotation_Y(state);
}
void GUIDataSolidWidget::on_checkBox_Freeze_Rotation_Z_stateChanged(int state)
{
	_obj->setFreeze_Rotation_Z(state);
}
